package com.ibest.pay.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibest.framework.common.enums.EnumsRtnMapResult;
import com.ibest.framework.common.persistence.BaseController;
import com.ibest.framework.common.utils.PageList;
import com.ibest.framework.common.utils.UserUtils;
import com.ibest.framework.system.entity.SysUser;
import com.ibest.pay.dto.input.PayTypeInputDTO;
import com.ibest.pay.entity.PayChannel;
import com.ibest.pay.entity.PayType;
import com.ibest.pay.service.PayChannelService;
import com.ibest.pay.service.PayTypeService;

@Controller
@RequestMapping(value="${adminPath}/pay/payType")
public class PayTypeController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(PayTypeController.class);
	
	@Autowired
	private PayTypeService payTypeService;
	
	@Autowired
	private PayChannelService payChannelService;
	
	/**
	* 进入到列表页
	*/
	@RequestMapping(value="/")
	public String index(){
		return "module/pay/payType/payTypeList";
	}
	
	/**
	* 进入到表单页-创建
	*/
	@RequestMapping(value="/add")
	public String add(Model model){
		List<PayChannel> payChannelList = payChannelService.findByAll();
		model.addAttribute("payChannelList", payChannelList);
		return "module/pay/payType/payTypeForm";
	}
	
	/**
	* 进入到表单页，编辑
	*/
	@RequestMapping(value="/edit")
	public String edit(@RequestParam String id, Model model){
		try {
			if(StringUtils.isNotEmpty(id)){
				PayType payType = payTypeService.findById(id);
				
				List<PayChannel> payChannelList = payChannelService.findByAll();
				model.addAttribute("payChannelList", payChannelList);
				if(payType != null){
					model.addAttribute("payType", payType);
				}else{
					setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "您查看的信息不存在！", model);
				}
			}else{
				setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "请选择需要编辑的信息！", model);
			}
		} catch (Exception e) {
			setRtnCodeAndMsg(EnumsRtnMapResult.EXCEPTION.getCode(), EnumsRtnMapResult.EXCEPTION.getMsg(), model);
		}
		return "module/pay/payType/payTypeForm";
	}
	
	/**
	* 进入到详情页
	*/
	@RequestMapping(value="/view")
	public String view(@RequestParam String id, Model model){
		try {
			if(StringUtils.isNotEmpty(id)){
				PayType payType = payTypeService.findById(id);
				if(payType != null){
					model.addAttribute("payType", payType);
				}else{
					setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "您查看的信息不存在！", model);
				}
			}else{
				setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "请选择需要查看的信息！", model);
			}
		} catch (Exception e) {
			setRtnCodeAndMsg(EnumsRtnMapResult.EXCEPTION.getCode(), EnumsRtnMapResult.EXCEPTION.getMsg(), model);
		}
		return "module/pay/payType/payTypeDetail";
	}

	/**
	* 异步分页查询
	*/
	@ResponseBody
	@RequiresPermissions("payType:query")
	@RequestMapping(value="/list")
	public PageList<PayType> list(PayTypeInputDTO payType, HttpServletRequest request){
		
		PageList<PayType> pageList = new PageList<PayType>();
		
		try {
			//设置分页参数
			super.setPage(request, pageList);
		
			pageList = payTypeService.findByPage(pageList, payType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageList;
	}
	
	/**
	* 异步表单提交
	*/
	@ResponseBody
	@RequiresPermissions("payType:create")
	@RequestMapping(value="create")
	public Map<String, Object> insert(PayType payType){
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		try {
			setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
			
			PayType pt = payTypeService.findByPayType(payType.getPayType());
			if(null!=pt && pt.getId()!=null) {
				setRtnCodeAndMsgByFailure(rtnMap, "支付类型编码已存在，请重新输入！");
				return rtnMap;
			}
			
			PayType py = payTypeService.findByPayName(payType.getPayName());
			if(null!=py && py.getId()!=null) {
				setRtnCodeAndMsgByFailure(rtnMap, "支付类型名称已存在，请重新输入！！");
				return rtnMap;
			}
			
			try {
				SysUser user = UserUtils.getCurrentUser();
				payType.setUpdateBy(user.getUpdateBy());
				payType.setCreateBy(user.getUsername());
				payType.setUpdateDate(new Date());
				payType.setCreateDate(new Date());
				int result = payTypeService.insert(payType);
				if(result == 0){
					setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				setRtnCodeAndMsgByException(rtnMap, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
		}	
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("payType:update")
	@RequestMapping(value="update")
	public Map<String, Object> update(PayType payType){

		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
		
		try {
			PayType pt = payTypeService.findByPayType(payType.getPayType());
			if(null!=pt && pt.getId()!=null) {
				if(!pt.getPayType().equals(payType.getPayType())) {
					setRtnCodeAndMsgByFailure(rtnMap, "支付类型编码已存在，请重新输入！");
					return rtnMap;
				}
			}
			
			PayType py = payTypeService.findByPayName(payType.getPayName());
			if(null!=py && py.getId()!=null) {
				if(!py.getPayName().equals(payType.getPayName())) {
					setRtnCodeAndMsgByFailure(rtnMap, "支付类型名称已存在，请重新输入！！");
					return rtnMap;
				}
			}
			SysUser user = UserUtils.getCurrentUser();
			payType.setUpdateBy(user.getUsername());
			payType.setUpdateDate(new Date());
			int result = payTypeService.update(payType);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("payType:delete")
	@RequestMapping(value="delete")
	public Map<String, Object> delete(@RequestParam(required=true) String ids){
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "删除成功");
		
		try {
			int result = payTypeService.deleteByIds(ids);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
	/**
	 * 全查询
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="payTypes")
	public List<PayType> payTypes(PayTypeInputDTO inputDto, HttpServletRequest request) throws Exception{
		return payTypeService.findByObjects(inputDto);
	}
	
	@ResponseBody
	@RequestMapping(value="/checkNames")
	public String checkNames(PayTypeInputDTO inputDto, HttpServletRequest request){
		try {
			List<PayType> p = payTypeService.findByNames(inputDto);
			if(null == p || p.size()==0) {
				return "true";
			}else {
				return "false";
			}
		} catch (Exception e) {
			return "true";
		}
	}
	
	/**
	 * 校验支付类型编码是否重复
	 * @Title: checkPayType  
	 * @param: @param payType
	 * @param: @param request
	 * @author: WeiJia
	 * @date:2018年5月8日 下午4:17:12
	 */
	@ResponseBody
	@RequestMapping(value="/checkPayType")
	public String checkPayType(String payType,HttpServletRequest request) {
		try {
			PayType pt = payTypeService.findByPayType(payType);
			if(pt!=null) {
				if(payType.equals(pt.getPayType())) {
					return "false";
				}else {
					return "true";
				}
			}else {
				return "true";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "true";
		}
	}
	
	/**
	 * 验证支付名称
	 * @Title: checkPayName  
	 * @param: @param payName
	 * @param: @param request
	 * @author: WeiJia
	 * @date:2018年5月8日 下午4:19:49
	 */
	@ResponseBody
	@RequestMapping(value="/checkPayName")
	public String checkPayName(String payName,HttpServletRequest request) {
		try {
			PayType pt = payTypeService.findByPayName(payName);
			if(pt!=null) {
				if(payName.equals(pt.getPayName())) {
					return "false";
				}else {
					return "true";
				}
			}else {
				return "true";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "true";
		}
	}
}
